/*
 * @Author: RYPY
 * @Date: 2021-03-24 09:15:38
 * @LastEditors: RYPY
 * @LastEditTime: 2021-03-24 17:20:01
 */
const express=require("express");
const umodel=require("../model/userModel");

const router=express.Router();

//登录功能接口
router.post("/login",async(req,res)=>{
   let {username,pwd}=req.body;
   if(await umodel.islogin({username,pwd})){
       res.send({code:200,msg:"登录成功",page:"userlist.html"});
   }else{
       res.send({code:201,msg:"登录失败"});
   } 
});
//注册功能接口
router.post("/reguest",async(req,res)=>{
    let {username,pwd}=req.body;
    try{
        await umodel.addUser({username,pwd});
        res.send({code:200,msg:"注册成功"});
    }catch{
        res.send({code:201,msg:"注册失败"});
    }
});
//获取用户列表功能接口
router.get("/getuserlist",async(req,res)=>{
    res.send({code:200,msg:"用户列表",userlist:await umodel.getUserList()}); 
 });
 //修改用户信息功能接口
 router.post("/updateuser",async(req,res)=>{
    let {userid,username,pwd,isadmin}=req.body;
    try{
        await umodel.updateUser({userid,username,pwd,isadmin});
        res.send({code:200,msg:"修改成功！"});
    } catch{
        res.send({code:201,msg:"修改失败！"});
    }
 });
 //删除用户信息功能接口
 router.post("/deluser",async(req,res)=>{
     let {userid}=req.body;
     try{
         await umodel.delUser(userid);
         res.send({code:200,msg:"删除成功！"});
     } catch{
         res.send({code:201,msg:"删除失败！"});
     }
 });
 
module.exports=router;